<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


class LotNumberFileLogic extends BaseLogic
{

    protected $autoCheckFields = false;

    protected $lotNumberFileModel;

    protected $lotNumberBallotModel;

    public function _initialize()
    {
        $this->lotNumberFileModel = D('LotNumberFile', 'Model');
        $this->lotNumberBallotModel = D('LotNumberBallot', 'Model');
    }

    /**
     * 摇号文件列表
     *
     * @param $page
     * @param $page_size
     * @param $params
     *
     * @return array
     */
    public function getLotNumberFileList($page, $page_size, $params)
    {
        $page = $page < 1 ? 1 : (int)$page;
        $page_size = $page_size < 1 ? 10 : (int)$page_size;
        $map = [];
        $map['status'] = ['NEQ', 0];
        if ($params['title']) {
            $map['title'] = ['LIKE', '%' . $params['title'] . '%'];
        }
        if ($params['city']) {
            $map['city_code'] = ['EQ', $params['city']];
        }
        if ($params['index_type']) {
            $map['index_type'] = ['eq', $params['index_type']];
        }
        if ($params['status']) {
            $map['status'] = ['eq', $params['status']];
        }
        if ($params['start_time']) {
            $map['create_time'] = ['egt', strtotime($params['start_time'] . ' 00:00:00')];
        }
        if ($params['end_time']) {
            $map['create_time'] = ['elt', strtotime($params['end_time'] . ' 23:59:59')];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['create_time'] = [
                ['egt', strtotime($params['start_time'] . ' 00:00:00')],
                ['elt', strtotime($params['end_time'] . ' 23:59:59')]
            ];
        }
        $cols = "*";
        $order = "id DESC";
        $total = $this->lotNumberFileModel->where($map)->count();
        $list = $this->lotNumberFileModel->field($cols)
            ->where($map)
            ->order($order)
            ->page($page)
            ->limit($page_size)
            ->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['index_type_name'] = C('LOT_NUMBER_INDEX_TYPE')[$v['index_type']];
                $list[$k]['city_name'] = C('CLCW_LOT_NUMBER_CITY')[$v['city_code']];
                $list[$k]['status_name'] = C('LOT_NUMBER_STATUS')[$v['status']];
                $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $list[$k]['deal_time'] = $v['deal_time'] > 0 ? date('Y-m-d H:i:s', $v['deal_time']) : '--';
            }
        }
        $result = [
            'list'  => $list,
            'total' => $total
        ];
        return $result;
    }

    /**
     * 添加文件
     *
     * @param array $data
     *
     * @return bool|mixed
     */
    public function addLotNumberFile($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $data['update_time'] = NOW_TIME;
        $data['create_time'] = NOW_TIME;
        return $this->lotNumberFileModel->add($data);
    }

    /**
     * 修改文件
     *
     * @param array $data
     *
     * @return bool|mixed
     */
    public function editLotNumberFile($data = [])
    {
        if (empty($data) || empty($data['id'])) {
            return false;
        }
        $data['update_time'] = NOW_TIME;
        return $this->lotNumberFileModel->save($data);
    }

    /**
     * 删除文件记录
     *
     * @param int $id
     *
     * @return bool
     */
    public function delLotNumberFile($id = 0)
    {
        if (empty($id) || $id < 1) {
            return false;
        }
        $res = $this->lotNumberFileModel->where(['id' => $id])->setField('status', 0);
        $this->lotNumberBallotModel->where(['lot_id' => $id])->setField('status', 0);
        return $res;
    }

    /**
     * 获取文件信息
     *
     * @param int $id
     *
     * @return bool|mixed
     */
    public function getLotNumberFileById($id = 0)
    {
        if (empty($id)) {
            return false;
        }
        return $this->lotNumberFileModel->find($id);
    }

    /**
     * 获取文件信息
     *
     * @param array $map
     */
    public function getLotNumberFile($map = [])
    {
        if (empty($map)) {
            return false;
        }
        return $this->lotNumberFileModel->where($map)->find();
    }
}